package com.bulletelc.apollopre.module.guide;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bulletelc.apollopre.MyApplication;
import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.SPKey;
import com.bulletelc.apollopre.framework.BaseActivity;
import com.bulletelc.apollopre.module.main.MainActivity;
import com.bulletelc.apollopre.utils.SharedPreferencesUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 首次启动引导页
 * Created by Administrator on 2018/4/21.
 */

public class GuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener,
        View.OnClickListener {

    private ViewPager vpGuide;
    private int[] imageIdArray;//图片资源的数组
    private List<View> viewList;//图片资源的集合
    private ViewGroup vgPoint;//放置圆点

    private TextView tvNext;


    //实例化原点View
    private ImageView ivPoint;
    private ImageView[] ivPointArray;

    @Override
    public int getLayoutRes() {
        return R.layout.guide_activity;
    }

    @Override
    public void assignViews() {
        vgPoint = findViewById(R.id.llPoint);
        vpGuide = findViewById(R.id.vpGuide);
        tvNext = findView(R.id.tvNext);
    }

    @Override
    public void setListener() {
        //设置滑动监听
        vpGuide.setOnPageChangeListener(this);
        tvNext.setOnClickListener(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        //加载ViewPager
        initViewPager();

        //加载底部圆点
        initPoint();
    }

    /**
     * 加载底部圆点
     */
    private void initPoint() {
        //这里实例化LinearLayout
        //根据ViewPager的item数量实例化数组
        ivPointArray = new ImageView[viewList.size()];
        //循环新建底部圆点ImageView，将生成的ImageView保存到数组中
        int size = viewList.size();
        for (int i = 0; i < size; i++) {
            ivPoint = new ImageView(this);
            ivPoint.setLayoutParams(new ViewGroup.LayoutParams(20, 20));
            ivPoint.setPadding(30, 0, 30, 0);//left,top,right,bottom
            ivPointArray[i] = ivPoint;
            //第一个页面需要设置为选中状态，这里采用两张不同的图片
            if (i == 0) {
                ivPoint.setBackgroundResource(R.drawable.guide_point_on);
            } else {
                ivPoint.setBackgroundResource(R.drawable.guide_point);
            }
            //将数组中的ImageView加入到ViewGroup
            vgPoint.addView(ivPointArray[i]);
        }

    }

    /**
     * 加载图片ViewPager
     */
    private void initViewPager() {
        //实例化图片资源
        imageIdArray = new int[]{R.drawable.guide};
        viewList = new ArrayList<>();
        //获取一个Layout参数，设置为全屏
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

        //循环创建View并加入到集合中
        int len = imageIdArray.length;
        for (int i = 0; i < len; i++) {
            //new ImageView并设置全屏和图片资源
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(params);
            imageView.setBackgroundResource(imageIdArray[i]);

            //将ImageView加入到集合中
            viewList.add(imageView);
        }

        //View集合初始化好后，设置Adapter
        vpGuide.setAdapter(new GuidePageAdapter(viewList));

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        //循环设置当前页的标记图
        int length = imageIdArray.length;
        for (int i = 0; i < length; i++) {
            ivPointArray[position].setBackgroundResource(R.drawable.guide_point_on);
            if (position != i) {
                ivPointArray[i].setBackgroundResource(R.drawable.guide_point);
            }
        }

        //判断是否是最后一页，若是则显示按钮
        if (position == imageIdArray.length - 1) {
            tvNext.setVisibility(View.VISIBLE);
        } else {
            tvNext.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvNext:
                Intent intent = new Intent(GuideActivity.this, MainActivity.class);
                startActivity(intent);
                SharedPreferencesUtil.putBoolean(MyApplication.getInstance(), SPKey.SP_FIRST_INSTALL, true);
                finish();
                break;
        }
    }
}
